<?php
class TemplatemanageAction extends Action{
   public function _initialize(){
	  header("content-type:text/html;charset=utf-8");
      //验证
	  check_pression();//检查权限
   }
   public function tpl(){//修改模板
      $tpl_path=rtrim(TMPL_PATH,"/");
	  if(!is_writeable($tpl_path))die($tpl_path."不可写");
	  $this->assign("dir_list",dir2arr($tpl_path));
	  $this->display();
   }
   public function lib(){//控制器和模型修改
      $lib_path=rtrim(LIB_PATH,"/");
	  if(!is_writeable($lib_path))die($lib_path."不可写");
	  $this->assign("dir_list",dir2arr($lib_path));
	  $this->display();
   }
   public function config(){//修改配置
      $config_path="config.php";
	  if(!is_writeable($config_path))die($config_path."不可写");
	  $this->assign("config_path",$config_path);
	  $this->assign("encode_path",base64_encode($config_path));
	  $this->display();
   }
   public function css(){//css修改
      $css_path="Public/Css";
	  if(!is_writeable($css_path))die($css_path."不可写");
	  $this->assign("dir_list",dir2arr($css_path));
	  $this->display();
   }
   public function lookcode(){
      //返回文件内容
	  $path=base64_decode($_GET["path"]);
	  if(empty($path))die("文件不存在");
      echo file_get_contents($path);
   }
   
   public function savecode(){
      //确保文件有写入权限
	  $filename=get_magic_quotes_gpc()?stripslashes($_POST["hidden_filename"]):$_POST["hidden_filename"];
	  $code_content=get_magic_quotes_gpc()?stripslashes($_POST["code_content"]):$_POST["code_content"];
	  @file_put_contents($filename,$code_content);
	  $this->redirect($_GET["type"]);
   }
}
function dir2arr($path){
   $dir_arr=array();
   if(empty($path)||!is_dir($path))die('$path不存在');
   $dir=@opendir($path);
   while(($file=@readdir($dir))!==false){
      if($file=="."||$file=="..") continue;
	  if(is_file($path."/".$file))array_push($dir_arr,$path."/".$file);
	  if(is_dir($path."/".$file))$dir_arr[$path."/".$file]=dir2arr($path."/".$file);
   }
   return $dir_arr; 
}
function show_dir($arr){
   if(empty($arr)) return false;
   if(is_array($arr)){
      foreach($arr as $k=>$v){
	     if(is_array($v)){
           echo "<li class='closed'><span class='folder'>$k</span>";
 		   echo "<ul>";
		   show_dir($v);
		   echo "</ul>";
		   echo "</li>";
		 }else{
		   $encode_path=base64_encode($v);
		   echo "<li><span class='file' encode_path='$encode_path' style='cursor:pointer;'>$v</span></li>";
		 }
	  }
   }else{
     $encode_path=base64_encode($v);
     echo "<li><span class='file' encode_path='$encode_path' style='cursor:pointer;'>$arr</span></li>";
   }
}
?>